package com.jilefojing.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Attitude;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.service.IAttitudeService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

/**
 * 态度
 * 
 * @author jgshun
 * 
 */
@Controller
@RequestMapping("attitude")
public class AttitudeWeb extends BaseWeb {
	@Autowired
	private IAttitudeService attitudeService;
	@Autowired
	private IStatisticsService statisticsService;

	@RequestMapping("add")
	public @ResponseBody ResponseData add(Attitude attitude, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			attitudeService.add(attitude, userContext);
			EnhancedStatistics enhancedStatistics = statisticsService.getEnhancedStatistics(ItemTypeEnumForSource.getItemTypeEnumByCode(attitude.getItemType()), attitude.getItemId(), userContext);
			Map<String, Object> resultMap = new HashMap<String, Object>();

			resultMap.put("statistics", CommonConvent.convent2StatisticsData(enhancedStatistics));
			responseData.setData(resultMap);
		} catch (UserContextException ex) {
			if (ex.getResponse() != null) {
				responseData.setResponseEnum(ex.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(ex.getMessage());
			}
		}

		return responseData;
	}

	@RequestMapping("cancle")
	public @ResponseBody ResponseData cancle(Attitude attitude, HttpServletRequest request) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			attitudeService.cancle(attitude, userContext);
			EnhancedStatistics enhancedStatistics = statisticsService.getEnhancedStatistics(ItemTypeEnumForSource.getItemTypeEnumByCode(attitude.getItemType()), attitude.getItemId(), userContext);
			Map<String, Object> resultMap = new HashMap<String, Object>();

			resultMap.put("statistics", CommonConvent.convent2StatisticsData(enhancedStatistics));
			responseData.setData(resultMap);
		} catch (UserContextException ex) {
			if (ex.getResponse() != null) {
				responseData.setResponseEnum(ex.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(ex.getMessage());
			}
		}

		return responseData;
	}

}
